/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.config.MyProps;
import com.jeesite.modules.goods.entity.ZtAddr;
import com.jeesite.modules.goods.entity.ZtGoods;
import com.jeesite.modules.goods.entity.ZtGoodsquan;
import com.jeesite.modules.goods.entity.ZtQuan;
import com.jeesite.modules.goods.service.ZtGoodsService;
import com.jeesite.modules.goods.service.ZtGoodsquanService;
import com.jeesite.modules.goods.service.ZtQuanService;

/**
 * 购物车Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztGoodsquan")
public class ZtGoodsquanController extends BaseController {

	@Autowired
	private ZtGoodsquanService ztGoodsquanService;
	
	@Autowired
	private ZtGoodsService ztGoodsService;
	
	@Autowired  
    private MyProps myProps;
	
	@Autowired
	ZtQuanService ztQuanService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtGoodsquan get(String id) {
		
		ZtGoodsquan ztGoodsquan= new ZtGoodsquan();
		if(StringUtils.isNotBlank(id)){
		ztGoodsquan.setId(id);
		ztGoodsquan=ztGoodsquanService.get(ztGoodsquan);
		}
		return ztGoodsquan;
	}

	/**
	 * 查询列表
	 */
	
	@RequestMapping(value = { "list", "" })
	public String list(ZtGoodsquan ztGoodsquan, Model model,HttpSession session) {
		try{
		if(StringUtils.isNotBlank(ztGoodsquan.getZtGoodsid())){
			session.setAttribute("ztGoodsquanid", ztGoodsquan.getZtGoodsid());
		}
		}catch(Exception ex){}
		model.addAttribute("ztGoodsquan", ztGoodsquan);
	
		return "modules/ztGoodsquan/ztGoodsquanList";
	}
	
	@RequestMapping(value ="setData")
	@ResponseBody
	public void setData(String selectData, Model model,HttpSession session,HttpServletRequest request,HttpServletResponse response) {
		ZtGoodsquan ztGoodsquan=new ZtGoodsquan();
		if(StringUtils.isBlank(ztGoodsquan.getZtGoodsid())){
			
			ztGoodsquan.setZtGoodsid(session.getAttribute("ztGoodsquanid").toString());
		
		}
		List<ZtGoodsquan> page = ztGoodsquanService.findList( ztGoodsquan);
		List<String> quanList=new ArrayList<String>();
		for(ZtGoodsquan zg:page){
			quanList.add(zg.getZtQuan().getId());
		}
		
		
		if(StringUtils.isNotBlank(selectData)&&selectData.length()>0){
			selectData=selectData.substring(1);
			String[] selectDataArr=selectData.split("&");
			for(int i=0;i<selectDataArr.length;i++){
				String selectQuanid=selectDataArr[i];
				if(quanList!=null&&quanList.size()>0){
					if(!quanList.contains(selectQuanid)){
						saveGoodsquan(ztGoodsquan.getZtGoodsid(),selectQuanid);
					}
				}else{
					saveGoodsquan(ztGoodsquan.getZtGoodsid(),selectQuanid);
				}
			}
		}
		
		
	
		JSONObject obj = new JSONObject();

		try {
			obj.put("result", "ok");
		} catch (JSONException e) {

			e.printStackTrace();

		}
		try {
			response.getWriter().print(obj.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//return "modules/ztGoodsquan/ztGoodsquanList";
	}
	
	public void saveGoodsquan(String ztGoodsid,String quanid){
		ZtGoodsquan zquan=new ZtGoodsquan();
		zquan.setZtGoodsid(ztGoodsid);
		ZtQuan q = new ZtQuan();
		q.setId(quanid);
		zquan.setZtQuan(q);
		ztGoodsquanService.save(zquan);
	}

	/**
	 * 查询列表数据
	 */
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtGoodsquan> listData(ZtGoodsquan ztGoodsquan, HttpServletRequest request,HttpSession session,
			HttpServletResponse response,Model model) {
		if(StringUtils.isBlank(ztGoodsquan.getZtGoodsid())){
			
			ztGoodsquan.setZtGoodsid(session.getAttribute("ztGoodsquanid").toString());
		
		}
		Page<ZtGoodsquan> page = ztGoodsquanService.findPage(new Page<ZtGoodsquan>(request,
				response), ztGoodsquan);
		
		
		
		//Page<ZtGoodsquan> page = ztGoodsquanService.findList(ztGoodsquan);	
		
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	
	/*@RequestMapping(value = "form")
	public String form(ZtGoodsquan ztGoodsquan, Model model,HttpServletRequest request,HttpServletResponse response) {
		
	//	model.addAttribute("ztGoodsquan", ztGoodsquan);	
				
		return "modules/ztGoodsquan/ztGoodsquanForm";
	}
	*/
	
	/**
	 * 查询列表
	 */
	
	@RequestMapping(value = "form")
	public String form(ZtQuan ztQuan, Model model,HttpSession session) {
		
		model.addAttribute("ztQuan", ztQuan);
	
		return "modules/ztGoodsquan/ztGoodsquanForm";
	}
	
	/**
	 * 查询列表数据
	 */

	@RequestMapping(value = "formList")
	@ResponseBody
	public Page<ZtQuan> formList(ZtQuan ztQuan, HttpServletRequest request,HttpSession session,
			HttpServletResponse response,Model model) {

			
		Page<ZtQuan> page = ztQuanService.findPage(new Page<ZtQuan>(request,
				response), ztQuan);
		if(page.getList()!=null && page.getList().size()>0){
		for(ZtQuan zd:page.getList()){
			if(StringUtils.isNotBlank(zd.getImg())){
			zd.setImg(myProps.getServerPath()+zd.getImg());
			}
		}
		}
		model.addAttribute("page",page);
		return page;
	}

	
	@RequestMapping(value = "getData")
	@ResponseBody
	public List<ZtQuan> getData(ZtQuan ztQuan, HttpServletRequest request,HttpSession session,
			HttpServletResponse response,Model model) {
		ZtGoodsquan ztGoodsquan=new ZtGoodsquan();
		if(StringUtils.isBlank(ztGoodsquan.getZtGoodsid())){
			
			ztGoodsquan.setZtGoodsid(session.getAttribute("ztGoodsquanid").toString());
		
		}
		Page<ZtGoodsquan> zgqPage = ztGoodsquanService.findPage(new Page<ZtGoodsquan>(request,
				response), ztGoodsquan);
		
		
		//List<ZtQuan> page = ztQuanService.findList(ztQuan);
		List<ZtQuan> page =new ArrayList<ZtQuan>();
		for(ZtGoodsquan zq:zgqPage.getList()){
			page.add(zq.getZtQuan());
		}
		
		for(ZtQuan zd:page){
			zd.setImg(myProps.getServerPath()+zd.getImg());
		}
		model.addAttribute("page",page);
		return page;
	}
	

	/**
	 * 保存数据
	 */
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtGoodsquan ztGoodsquan,HttpSession session) {
		ztGoodsquan.setCreateDate(new Date());
		
		if(StringUtils.isBlank(ztGoodsquan.getZtGoodsid())){
			
			ztGoodsquan.setZtGoodsid(session.getAttribute("ztGoodsquanid").toString());
		}
		
		ztGoodsquanService.save(ztGoodsquan);
		return renderResult(Global.TRUE, text("保存成功！"));
	}

	
	/**
	 * 删除数据
	 */

	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtGoodsquan ztGoodsquan) {
		ztGoodsquanService.delete(ztGoodsquan);
		return renderResult(Global.TRUE, text("删除成功！"));
	}
}